package com.yanqu.road.dao.impl.cross.tradewar;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.rank.UnionActivityRank;
import com.yanqu.road.entity.union.UnionBaseInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class CrossTradeWarUnionRankDaoImpl extends TempDao {

    public Map<Integer, LinkedList<UnionActivityRank>> getCrossTradeWarUnionRankListMap(List<Integer> activityIdList) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, LinkedList<UnionActivityRank>> dataMap = new ConcurrentHashMap<>();
        if(null == activityIdList || activityIdList.size() <= 0){
            return dataMap;
        }
        String sql = "select * from t_c_trade_war_union_rank where activityId in(" + StringUtils.listToString(activityIdList, ",") + ")";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                UnionActivityRank data = initUnionActivityRank(rs);
                if(!dataMap.containsKey(data.getActivityId())){
                    dataMap.put(data.getActivityId(), new LinkedList<>());
                }
                dataMap.get(data.getActivityId()).add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public boolean updateCrossTradeWarUnionRankList(int activityId, List<UnionActivityRank> rankList) {
        String sql = "REPLACE INTO t_c_trade_war_union_rank(activityId,rank,unionUid,unionBaseInfo,lastUpdateTime,value) VALUES(?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
            int rank = 1;
            for (UnionActivityRank unionRank : rankList) {
                int i = 1;
                Map<Integer, DbParameter> para = new HashMap<>();
                para.put(i++, new DbParameter(Types.INTEGER, activityId));
                para.put(i++, new DbParameter(Types.INTEGER, rank));
                para.put(i++, new DbParameter(Types.VARCHAR, unionRank.getUnionUid()));
                para.put(i++, new DbParameter(Types.NVARCHAR, unionRank.getUnionBaseInfoJsonStr()));
                para.put(i++, new DbParameter(Types.BIGINT, unionRank.getLastUpdateTime()));
                para.put(i++, new DbParameter(Types.VARCHAR, unionRank.getValue().toString()));
                mapList.add(para);
                rank++;
            }
            if (getDb().executeBatchQuery(sql, mapList) > -1) {
                return true;
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        }
        return false;
    }

    private UnionActivityRank initUnionActivityRank(ResultSet rs) throws SQLException {
        UnionActivityRank data = new UnionActivityRank();
        data.setActivityId(rs.getInt("activityId"));
        data.setUnionUid(rs.getString("unionUid"));
        data.setLastUpdateTime(rs.getLong("lastUpdateTime"));
        String value = rs.getString("value");
        if(!StringUtils.isNullOrEmpty(value)) {
            data.setValue(new BigInteger(value));
        }
        value = rs.getString("unionBaseInfo");
        if(null == value) {
            value = "";
        }
        UnionBaseInfo unionBaseInfo = JSON.parseObject(value, new TypeReference<UnionBaseInfo>() {});
        if(null != unionBaseInfo){
            data.setUnionBaseInfo(unionBaseInfo);
        }
        return data;
    }
}
